package com.treetao.union.mall.api.base;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.treetao.union.core.exception.DevMException;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.mall.api.vo.req.ReqParam;
import com.treetao.union.mall.api.vo.resp.PageResp;

/**
 * 继续该类，默认拥有了 分页，列表，详情，删除 的方法。 子类可根据需要，覆盖相应的方法来构建不同的查询条件
 * 
 * @Author devm
 * @date 2020年5月27日
 * @see https://gitee.com/devm/devm-mall-server
 */
public abstract class BaseCURDController extends BaseApiController {
	/**
	 * 当前公用业务Service
	 */
	protected abstract <T> IService<T> getCommonService();

	/**
	 * 当前公用业务是否需要鉴权
	 */
	protected boolean setCommonDataAuth() {
		return true;
	}

	/** 列表查询条件 */
	protected void commonListWhere(QueryWrapper<?> q, ReqParam param) {
	};

	/** 分页查询条件 */
	protected void commonPageWhere(QueryWrapper<?> q, ReqParam param) {
	};

	protected static final String list = "commonlist";
	protected static final String page = "commonpage";
	protected static final String detail = "commondetail";
	protected static final String delete = "commondelete";

	public ReqParam getMappingReqParam() {
		return new ReqParam();
	}

	/**
	 * API删除
	 * 
	 * @return
	 */
	@GetMapping(delete)
	protected RestResponse delete() {
		return RestResponse.success(getCommonService().removeById(getMappingReqParam().getLong("id", false)));
	}

	/**
	 * API列表
	 * 
	 * @return
	 */
	@GetMapping(list)
	protected RestResponse list() {
		return RestResponse.success(getListData());
	}
	
	/**
	 * API分页
	 * 
	 * @param param
	 * @return
	 */
	@GetMapping(page)
	protected RestResponse page() {
		return RestResponse.success(getPageData());
	}

	/**
	 * API详情
	 * 
	 * @param param
	 * @return
	 */
	@GetMapping(detail)
	protected RestResponse detail() {
		if (getCommonService() == null) {
			return RestResponse.failure("请配置CommonService");
		}
		if (setCommonDataAuth()) {
			getWxuidFromToken();
		}
		return RestResponse.success(getCommonService().getById(getMappingReqParam().getLong("id", false)));
	}
	
	/**
	 * 获取列表数据
	 * @return
	 */
	protected List<?> getListData() {
		if (getCommonService() == null) {
			throw new DevMException("请配置CommonService");
		}
		if (setCommonDataAuth()) {
			getWxuidFromToken();
		}
		QueryWrapper<Object> q = getQueryWrapperNoDel();
		commonListWhere(q, getMappingReqParam());
		return  getCommonService().list(q);
	}

	/**
	 * 获取最终分页数据
	 * 
	 * @return
	 */
	protected PageResp getPageData() {
		if (getCommonService() == null) {
			throw new DevMException("请配置CommonService");
		}
		if (setCommonDataAuth()) {
			getWxuidFromToken();
		}
		QueryWrapper<?> q = getQueryWrapperNoDel();
		ReqParam rp = getMappingReqParam();
		commonPageWhere(q, rp);
		return pageData(rp, q);
	}

	/**
	 * 从数据库查询分页数据
	 * 
	 * @param param
	 * @param q
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private PageResp pageData(ReqParam param, @SuppressWarnings("rawtypes") QueryWrapper q) {
		IPage<Object> ipage = new Page<Object>(param.getPageCurrent(), param.getPageSize());
		IPage<Object> page = getCommonService().page(ipage, q);
		PageResp resp = new PageResp();
		resp.setCurrent(param.getPageCurrent());
		resp.setItems(page.getRecords());
		resp.setHasNextPage(page.getPages() > param.getPageCurrent());
		return resp;
	}

	/**
	 * 自定义查询时，封装分页结果对象
	 * 
	 * @param ipage
	 * @return
	 */
	protected PageResp conver2pageData(IPage<?> ipage) {
		PageResp resp = new PageResp();
		resp.setCurrent(ipage.getCurrent());
		resp.setItems(ipage.getRecords());
		resp.setHasNextPage(ipage.getPages() > ipage.getCurrent());
		return resp;
	}

}
